package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.domain.Callback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 【异步调用】异步获取机器人好友列表接口（仅PC可用）回调
 * <p>
 * 1、机器人登录后，平台分批回调好友列表（3022），每批1000个好友，直至全部回调完成；
 * <p>
 * 2、机器人在不掉线的情况下每24小时，平台主动分批推送好友列表（3022）用于商家校验；
 * <p>
 * 【主动推送好友列表】
 * <pre>
 * {
 *     "vcMerchantNo": "202309130012311",
 *     "vcRobotWxId": "wxid_mhryqsn7abpa22",
 *     "vcRobotSerialNo": "FBCEE80FC5340B689E8B517B5B7805FF",
 *     "vcSerialNo": "20250113165500662480210604262",
 *     "nType": 3022,
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "nTimeStamp": 1736758500701,
 *     "Data": {
 *         "PageIndex": 0,
 *         "IsLastPage": true,
 *         "FriendList": [
 *             {
 *                 "vcFriendWxId": "filehelper",
 *                 "vcFriendSerialNo": "9E20F478899DC29EB19741386F9343C8",
 *                 "nSex": 0,
 *                 "vcWxAlias": "filehelper",
 *                 "vcNickName": "文件传输助手",
 *                 "vcBase64NickName": "5paH5Lu25Lyg6L6T5Yqp5omL",
 *                 "vcHeadImgUrl": "https://wx.qlogo.cn/mmhead/ver_1/6XjAiaPRhbpjh9ru29FrP3Ysm5sOqoiapM5OGXyRfiaqJQ2oZrFop7WylCnicicoChlxickonS1tGrZS0QtToMqicYksTsZhFc9DiaDUicV0GgMErJV0/132",
 *                 "vcRemarkName": "文件助手",
 *                 "vcBase64RemarkName": "5paH5Lu25Yqp5omL",
 *                 "vcCountry": "",
 *                 "vcProvince": "",
 *                 "vcCity": "",
 *                 "nBlockStatus": 10,
 *                 "vcTagIds": ""
 *             },
 *             {
 *                 "vcFriendWxId": "wxid_s7fzp0gmouhf21",
 *                 "vcFriendSerialNo": "704EF03ABA594D37196B97139D3DF0D4",
 *                 "nSex": 2,
 *                 "vcWxAlias": "love15307247446",
 *                 "vcNickName": "杨玲罗",
 *                 "vcBase64NickName": "5p2o546y572X",
 *                 "vcHeadImgUrl": "https://wx.qlogo.cn/mmhead/ver_1/uDibU9UZICRlJVXRtRI0VlG5kDkQlCN6VswGuqGbv7LLLbtIb7F1LxZ6XPGp9YyDASBTrkNEgia83g3jUib7UABs8oHf8OjicBUZme1POSe3ywY/96",
 *                 "vcRemarkName": "",
 *                 "vcBase64RemarkName": "",
 *                 "vcCountry": "CN",
 *                 "vcProvince": "Hubei",
 *                 "vcCity": "Xianning",
 *                 "nBlockStatus": 0,
 *                 "vcTagIds": ""
 *             },
 *             {
 *                 "vcFriendWxId": "wxid_ld3133n6wjy022",
 *                 "vcFriendSerialNo": "FE5318EBB7DA859E67CBB48DF0762A2D",
 *                 "nSex": 1,
 *                 "vcWxAlias": "wz99209",
 *                 "vcNickName": "爱在心中(彩票店）",
 *                 "vcBase64NickName": "54ix5Zyo5b+D5LitKOW9qeelqOW6l++8iQ==",
 *                 "vcHeadImgUrl": "https://wx.qlogo.cn/mmhead/ver_1/QRwNrmEzjGLjVhUOYXC4kk2xlwCQzsQT4icCHzUdGmYpuWv9heib5bOhBao4BWibuiaomI6prBDH0M404gV3Vt4lo9rmmvUJPpAQ97DwInwTOGjoIGKc1M9vGoFRGTicVxFQJpxqfib76f8z9t236r7bRJLQ/96",
 *                 "vcRemarkName": "",
 *                 "vcBase64RemarkName": "",
 *                 "vcCountry": "CN",
 *                 "vcProvince": "Hubei",
 *                 "vcCity": "Jingzhou",
 *                 "nBlockStatus": 0,
 *                 "vcTagIds": ""
 *             }
 *         ]
 *     }
 * }
 * </pre>
 */
@Slf4j
@Component
public class _3022Handler extends RobotContactHandler implements CallbackHandler {

    @Override
    public String name() {
        return "异步获取机器人好友列表接口（仅PC可用）回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 3022;
    }

    @Override
    public void handle(Callback callback) {
        LocalDateTime now = LocalDateTime.now();
        JSONObject data = (JSONObject) callback.getData();
        JSONArray friends = data.getJSONArray("FriendList");    // 数据只有一页
        for (Object friend : friends) {
            saveRobotContact(callback, JSONObject.from(friend), now);
        }
        log.info("异步获取机器人好友列表完成[serialNo:{}, robotId:{}, robotSerialNo:{}]",
                callback.getSerialNo(), callback.getRobotId(), callback.getRobotSerialNo());
    }
}
